package result;

import idx.Extractor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

//import ruc.irm.similarity.word.hownet2.concept.BaseConceptParser;
import datastructure.sentence;
import datastructure.word;

import ICTCLAS.I3S.AC.ICTCLAS50;

public class FeatureExtractor {
	//Extractor ex;
	public static double ftrCal(sentence a, String b, String filename) throws IOException
	{
		return 0.8 * wordSim(a, b, filename) + 0.2 * wordLen(a.content, b);
	}
	public static int EditDistance(String str1,String str2)
	{

	         char[] strs1=str1.toCharArray();//转换为字符数组
	         char[] strs2=str2.toCharArray();//转换为字符数组
	         int[][] dist=new int[strs1.length+1][strs2.length+1];//定义距离二维数组,多定义一维是为了避免边界检测
	         int i,j,temp;
	         for(i=0;i<=strs1.length;i++) dist[i][0]=0;//初始化边界值
	         for(i=0;i<=strs2.length;i++) dist[0][i]=0;//初始化边界值
	         for(i=1;i<=strs1.length;i++)
	         {
	                   for(j=1;j<=strs2.length;j++)
	                   {                         
	                            if(strs1[i-1]==strs2[j-1])//如果两字符相等，则取dist[i-1][j-1]+1
	                            {
	                                     temp=dist[i-1][j-1]+1;
	                            }
	                            else//不等，则取dist[i-1][j]与dist[i][j-1]的最大值
	                            {
	                                     temp=dist[i][j-1];
	                                     if(dist[i-1][j]>temp) temp=dist[i-1][j];
	                            }
	                            dist[i][j]=temp;
	                   }
	         }
	         temp=dist[strs1.length][strs2.length];
	         if(strs1.length>strs2.length)
	                   return strs1.length-temp;
	         else
	                   return strs2.length-temp;
	}
	public static double wordSim(sentence a, String b, String filename) throws IOException
	{
		//a = splitWords(a, 0);
		//System.out.println(a.size());
		
		b = splitWords(b, 0);
		//String []groupA = a.split(" ");
		String []groupB = b.split(" ");
		//boolean []match = new boolean[b.size()];
		boolean []match = new boolean[groupB.length];
		double keyWordLen = 0;
		for(int i = 0; i < a.keyword.size(); i++)
		{
			if(a.keyword.get(i).pos.startsWith("nr") || a.keyword.get(i).pos.startsWith("ns"))
				keyWordLen += 2*Math.log(a.keyword.get(i).content.length() + 1);
			else
				keyWordLen += Math.log(a.keyword.get(i).content.length() + 1);
		}
		for(int i = 0; i < match.length; i++)
			match[i] = false;
		double result = 0;
		for(int i = 0; i < a.keyword.size(); i++)
			for(int j = 0; j < groupB.length; j++)
			{
				if(groupB[j].equals(a.keyword.get(i).content) && match[j] == false)
				{
					if(a.keyword.get(i).pos.startsWith("ns") || a.keyword.get(i).pos.startsWith("nr"))
						result += 2*Math.log(a.keyword.get(i).content.length() + 1);
					else
						result += Math.log(a.keyword.get(i).content.length() + 1);
					//result+= groupB[j].length();
					match[j] = true;
					break;
				}
			}
		result = result / keyWordLen;
		if(filename == null)
			return result;
		//Extractor ex = new Extractor();
		//ex.buildConvertTable("table.txt");
		//String f = 
		//for(int i = 0; i < a.keyword.size(); i++)
		//{
			//if(filename.contains(a.keyword.get(i).content))
				//result += 0.2;
		//}
		return result;
	}
	public static double wordLen(String a, String b)
	{
		double lena = a.length();
		double lenb = b.length();
		return 1 - Math.abs(lena-lenb)/(lena+lenb);
	}
	public static String splitWords(String ar, int tag) throws IOException
	{
		//tag = 1, tag ; tag = 0, no tag
		ICTCLAS50 testICTCLAS50 = new ICTCLAS50();
		String argu = ".";
		//初始化
		if (testICTCLAS50.ICTCLAS_Init(argu.getBytes("UTF8")) == false)
		{
			System.out.println("Init Fail!");
			return null;
		}


		//设置词性标注集(0 计算所二级标注集，1 计算所一级标注集，2 北大二级标注集，3 北大一级标注集)
		testICTCLAS50.ICTCLAS_SetPOSmap(2);


		//导入用户词典前分词
		byte nativeBytes[] = testICTCLAS50.ICTCLAS_ParagraphProcess(ar.getBytes("UTF8"), 0, tag);//分词处理
		//System.out.println(nativeBytes.length);
		String nativeStr = new String(nativeBytes, 0, nativeBytes.length, "UTF8");
		//System.out.println(nativeStr);//打印结果
		return nativeStr;
	}
}
